package com.social.socialservice.dal;

import com.social.socialservice.model.UserInfo;
import com.social.socialservice.unit.RandomPassHelper;
import com.social.socialservice.unit.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

/**
 * 邮件模块
 */

@Service
public class EmailService {

    @Autowired
    UserDAL userDAL;

    @Resource
    private JavaMailSender jms;

    /**
     * 封装发送html邮件方法
     * @param sender
     * @param receiver
     * @param subject
     * @param content
     * @return
     */
    public String sendHtml(String sender, String receiver, String subject, String content) {
        MimeMessage message = jms.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            //发送方
            helper.setFrom(sender);
            //接收方
            helper.setTo(receiver);
            //邮件主题
            helper.setSubject(subject);
            //邮件内容
            helper.setText(content, true);
            jms.send(message);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
        return "邮件发送成功，请查收邮件！";
    }

    /**
     * 根据用户时输入的邮箱地址发送验证码
     * @param userInfo
     * @return
     */
    public Response sendValidateCode(UserInfo userInfo){
        if(userInfo!=null){
            //发送者
            String sender = "317834473@qq.com";
            //标题
            String subject = "暄语社交平台注册验证码";
            //这个是接受人的邮箱
            String receiver = userInfo.getUser_email();
            //验证码6位
            String reset_code = RandomPassHelper.getPassWordOne(6);
            userInfo.setCode_string(reset_code);
            userDAL.setValidateCode(userInfo);
            //邮件内容
            String content = "<html>\n" +
                    "<body>\n" +
                    "尊敬的用户：" +
                    "<h2>这是一封注册验证邮件,请使用下方验证码（区分大小写）进行注册!</h2>\n" +
                    "<p>提示：验证码有效时间15分钟。</p>" +
                    "<p style=\"background-color: #000000;color: #ffffff;font-size: 50px;width: 300px;height: 120px;line-height: 120px;text-align: center;\">" +
                    reset_code +
                    "</p>"+
                    "<p>注意：如果该邮件不是您操作发送，请忽视该邮件。</p>" +
                    "</body>\n" +
                    "</html>";
            return new Response(200,sendHtml(sender, receiver, subject, content),null);
        }else {
            return new Response(401, "发送注册验证码失败", null);
        }
    }


    public Response resetAndSendMail(UserInfo userInfo) {
        UserInfo user =userDAL.getResetLink(userInfo);
        if (user != null) {
            //验证通过
            //发送者
            String sender = "317834473@qq.com";
            //标题
            String subject = "暄语社交平台密码找回";
            //这个是接受人的邮箱
            String receiver = user.getUser_email();
            //重置码6位
            String reset_code = RandomPassHelper.getPassWordOne(6);
            user.setUser_code(reset_code);
            userDAL.resetCode(user);
            //邮件内容
            String content = "<html>\n" +
                    "<body>\n" +
                    "尊敬的用户：" +
                    "<h2>这是一封密码找回邮件,请使用下方验证码（区分大小写）重置登录密码!</h2>\n" +
                    "<p>提示：验证码有效时间15分钟。</p>" +
                    "<p style=\"background-color: #000000;color: #ffffff;font-size: 50px;width: 300px;height: 120px;line-height: 120px;text-align: center;\">" +
                    reset_code +
                    "</p>"+
                    "<p>注意：如果该邮件不是您操作发送，请忽视该邮件。</p>" +
                    "</body>\n" +
                    "</html>";
            return new Response(200,sendHtml(sender, receiver, subject, content),null);
        } else {
            return new Response(401, "密码重置验证失败", null);
        }
    }
}
